﻿using AutoMapper;
using Jinjia.Approve.Entity;
using Jinjia.Approve.Entity.Entity;
using Jinjia.Approve.Entity.Entity.JJFlow;
using Jinjia.Approve.Model.Input.JJFlow;
using Jinjia.Approve.Model.VM;
using Jinjia.Approve.Model.VM.JJFlow;

namespace Jinjia.Approve.Model.AutoMapperProfile
{
    public class JJFlowProfile : Profile
    {
        public JJFlowProfile()
        {
            SourceMemberNamingConvention = new IgnoreCaseNamingConvention();
            DestinationMemberNamingConvention = new IgnoreCaseNamingConvention();

            CreateMap<jj_flow_mapping_tbl, JjFlowMappingTblVM>()
                .ForMember(des => des.SystemKey, opt => opt.MapFrom(x => x.system_key))
                .ForMember(des => des.TemplateId, opt => opt.MapFrom(x => x.template_id))
                .ForMember(des => des.BigModularKey, opt => opt.MapFrom(x => x.big_modular_key))
                .ForMember(des => des.ModularKey, opt => opt.MapFrom(x => x.modular_key))
                .ForMember(des => des.ModularName, opt => opt.MapFrom(x => x.modular_name))
                .ForMember(des => des.Remark, opt => opt.MapFrom(x => x.remark))
                .ReverseMap();

            CreateMap<jj_flow_instance, JjFlowInstanceVM>()
                .ForMember(des => des.Id, opt => opt.MapFrom(x => x.id))
                .ForMember(des => des.TemplateId, opt => opt.MapFrom(x => x.template_id))
                .ForMember(des => des.CurrentNodeId, opt => opt.MapFrom(x => x.current_node_id))
                .ForMember(des => des.EndNodeId, opt => opt.MapFrom(x => x.end_node_id))
                .ForMember(des => des.ModularKey, opt => opt.MapFrom(x => x.modular_key))
                .ForMember(des => des.Status, opt => opt.MapFrom(x => x.status))
                .ForMember(des => des.StartUser, opt => opt.MapFrom(x => x.start_user))
                .ForMember(des => des.EndUser, opt => opt.MapFrom(x => x.end_user))
                .ForMember(des => des.BizOrderId, opt => opt.MapFrom(x => x.biz_order_id))
                .ForMember(des => des.Remark, opt => opt.MapFrom(x => x.remark))
                .ForMember(des => des.OperationType, opt => opt.MapFrom(x => x.operation_type))
                .ForMember(des => des.CreateUserId, opt => opt.MapFrom(x => x.create_user_id))
                .ForMember(des => des.CreateTime, opt => opt.MapFrom(x => x.create_time))
                .ForMember(des => des.UpdateUserId, opt => opt.MapFrom(x => x.update_user_id))
                .ForMember(des => des.UpdateTime, opt => opt.MapFrom(x => x.update_time))
                .ReverseMap();

            CreateMap<jj_node, Node>()
                .ForMember(des => des.id, opt => opt.MapFrom(x => x.id))
                .ForMember(des => des.template_id, opt => opt.MapFrom(x => x.template_id))
                .ForMember(des => des.node_name, opt => opt.MapFrom(x => x.node_name))
                .ForMember(des => des.node_type, opt => opt.MapFrom(x => x.node_type))
                .ForMember(des => des.node_step, opt => opt.MapFrom(x => x.node_step))
                .ForMember(des => des.template_name, opt => opt.MapFrom(x => x.template_name))
                .ForMember(des => des.to_node, opt => opt.MapFrom(x => x.to_node))
                .ForMember(des => des.is_return, opt => opt.MapFrom(x => x.is_return))
                .ForMember(des => des.remark, opt => opt.MapFrom(x => x.remark))
                .ForMember(des => des.emp_type, opt => opt.MapFrom(x => x.emp_type))
                .ForMember(des => des.class_name, opt => opt.MapFrom(x => x.class_name))
                .ForMember(des => des.bpm_nodeid, opt => opt.MapFrom(x => x.bpm_nodeid))
                .ForMember(des => des.power_type, opt => opt.MapFrom(x => x.power_type))
                .ForMember(des => des.power_org_level, opt => opt.MapFrom(x => x.power_org_level))
                .ForMember(des => des.auto_approve, opt => opt.MapFrom(x => x.auto_approve))
                .ForMember(des => des.approve_rule, opt => opt.MapFrom(x => x.approve_rule))
                .ForMember(des => des.reject_rule, opt => opt.MapFrom(x => x.reject_rule))
                .ReverseMap();

            CreateMap<jj_flow_template, JjFlowTemplateVM>()
                .ForMember(des => des.Id, opt => opt.MapFrom(x => x.id))
                .ForMember(des => des.TemplateName, opt => opt.MapFrom(x => x.template_name))
                .ForMember(des => des.TemplateType, opt => opt.MapFrom(x => x.template_type))
                .ForMember(des => des.IsSubmitUserCheck, opt => opt.MapFrom(x => x.is_submit_user_check))
                .ForMember(des => des.Remark, opt => opt.MapFrom(x => x.remark))
                .ForMember(des => des.IsRevoke, opt => opt.MapFrom(x => x.is_revoke))
                .ForMember(des => des.DetailPageId, opt => opt.MapFrom(x => x.detail_page_id))
                .ForMember(des => des.DetailPageParameterKey, opt => opt.MapFrom(x => x.detail_page_parameter_key))
                .ForMember(des => des.is_push_feishu, opt => opt.MapFrom(x => x.is_push_feishu))
                .ReverseMap();

            CreateMap<jj_flow_step, JjFlowStepVM>()
                .ForMember(des => des.Id, opt => opt.MapFrom(x => x.id))
                .ForMember(des => des.TemplateId, opt => opt.MapFrom(x => x.template_id))
                .ForMember(des => des.FromNode, opt => opt.MapFrom(x => x.from_node))
                .ForMember(des => des.ToNode, opt => opt.MapFrom(x => x.to_node))
                .ForMember(des => des.CurrentNode, opt => opt.MapFrom(x => x.current_node))
                .ForMember(des => des.InstanceId, opt => opt.MapFrom(x => x.instance_id))
                .ForMember(des => des.CurrentUser, opt => opt.MapFrom(x => x.current_user))
                .ForMember(des => des.IsConditionSatified, opt => opt.MapFrom(x => x.is_condition_satified))
                .ForMember(des => des.ConditionName, opt => opt.MapFrom(x => x.condition_name))
                .ForMember(des => des.StepType, opt => opt.MapFrom(x => x.step_type))
                .ForMember(des => des.BpmNodeid, opt => opt.MapFrom(x => x.bpm_nodeid))
                .ForMember(des => des.ConditionType, opt => opt.MapFrom(x => x.condition_type))
                .ForMember(des => des.Remark, opt => opt.MapFrom(x => x.remark))
                .ForMember(des => des.Priority, opt => opt.MapFrom(x => x.priority))
                .ReverseMap();

            CreateMap<jj_flow_track, JjFlowTrackVM>()
                .ForMember(des => des.id, opt => opt.MapFrom(x => x.id))
                .ForMember(des => des.instance_id, opt => opt.MapFrom(x => x.instance_id))
                .ForMember(des => des.operation_result, opt => opt.MapFrom(x => x.operation_result))
                .ForMember(des => des.node_id, opt => opt.MapFrom(x => x.node_id))
                .ForMember(des => des.node_name, opt => opt.MapFrom(x => x.node_name))
                .ForMember(des => des.action_type, opt => opt.MapFrom(x => x.action_type))
                .ForMember(des => des.action_type_name, opt => opt.MapFrom(x => x.action_type_name))
                .ForMember(des => des.from_node, opt => opt.MapFrom(x => x.from_node))
                .ForMember(des => des.from_node_name, opt => opt.MapFrom(x => x.from_node_name))
                .ForMember(des => des.to_node, opt => opt.MapFrom(x => x.to_node))
                .ForMember(des => des.to_node_name, opt => opt.MapFrom(x => x.to_node_name))
                .ForMember(des => des.from_emp, opt => opt.MapFrom(x => x.from_emp))
                .ForMember(des => des.to_emp, opt => opt.MapFrom(x => x.to_emp))
                .ForMember(des => des.remark, opt => opt.MapFrom(x => x.remark))
                .ForMember(des => des.create_user_id, opt => opt.MapFrom(x => x.create_user_id))
                .ForMember(des => des.create_time, opt => opt.MapFrom(x => x.create_time))
                .ForMember(des => des.update_user_id, opt => opt.MapFrom(x => x.update_user_id))
                .ForMember(des => des.update_time, opt => opt.MapFrom(x => x.update_time))
                .ForMember(des => des.node_status_type, opt => opt.MapFrom(x => x.node_status_type))
                .ForMember(des => des.emp_name, opt => opt.MapFrom(x => x.emp_name))
                .ReverseMap();

            CreateMap<Node, JjFlowTrackVM>()
                .ForMember(des => des.id, opt => opt.MapFrom(x => x.id))
                .ForMember(des => des.node_name, opt => opt.MapFrom(x => x.node_name))
                .ForMember(des => des.remark, opt => opt.MapFrom(x => x.remark))
                .ForMember(des => des.emp_type, opt => opt.MapFrom(x => x.emp_type))
                .ForMember(des => des.to_node, opt => opt.MapFrom<JjFlowTrackVMResolver>())
                .ReverseMap();

            CreateMap<CreateRequest, AutoApproveRequest>()
                .ForMember(des => des.modular_key, opt => opt.MapFrom(x => x.modular_key))
                .ForMember(des => des.biz_order_id, opt => opt.MapFrom(x => x.biz_order_id))
                .ReverseMap();

            CreateMap<ErpaViewedLogTblVM, erpa_viewed_log_tbl>()
                .ForMember(des => des.total, opt => opt.MapFrom(x => x.total))
                .ForMember(des => des.biz_order_id, opt => opt.MapFrom(x => x.biz_order_id))
                .ForMember(des => des.doc_model, opt => opt.MapFrom(x => x.doc_model))
                .ReverseMap();

            CreateMap<jj_flow_dictcode, JjFlowDictcodeVM>()
                .ForMember(des => des.id, opt => opt.MapFrom(x => x.id))
                .ForMember(des => des.create_time, opt => opt.MapFrom(x => x.create_time))
                .ForMember(des => des.create_user_id, opt => opt.MapFrom(x => x.create_user_id))
                .ForMember(des => des.update_time, opt => opt.MapFrom(x => x.update_time))
                .ForMember(des => des.deleted, opt => opt.MapFrom(x => x.deleted))
                .ForMember(des => des.dictcode_name, opt => opt.MapFrom(x => x.dictcode_name))
                .ForMember(des => des.template_id, opt => opt.MapFrom(x => x.template_id))
                .ForMember(des => des.dictcode_key, opt => opt.MapFrom(x => x.dictcode_key))
                .ForMember(des => des.remark, opt => opt.MapFrom(x => x.remark))
                .ForMember(des => des.is_push_feishu, opt => opt.MapFrom(x => x.is_push_feishu))
                .ForMember(des => des.sort_number, opt => opt.MapFrom(x => x.sort_number))
                .ReverseMap();

            CreateMap<jj_node, JjFlowTrackVM>()
                .ForMember(des => des.node_name, opt => opt.MapFrom(x => x.node_name))
                .ForMember(des => des.to_node, opt => opt.MapFrom(x => string.IsNullOrEmpty(x.to_node) ? 0 : int.Parse(x.to_node)))
                .ForMember(des => des.remark, opt => opt.MapFrom(x => x.remark))
                .ForMember(des => des.id, opt => opt.MapFrom(x => x.id))
                .ForMember(des => des.create_time, opt => opt.MapFrom(x => x.create_time))
                .ForMember(des => des.create_user_id, opt => opt.MapFrom(x => x.create_user_id))
                .ForMember(des => des.update_time, opt => opt.MapFrom(x => x.update_time))
                .ForMember(des => des.update_user_id, opt => opt.MapFrom(x => x.update_user_id))
                .ReverseMap();

            CreateMap<jj_node_reference, JJFlowNodeReferenceVM>()
                .ForMember(des => des.Id, opt => opt.MapFrom(x => x.id))
                .ForMember(des => des.FlowInstanceId, opt => opt.MapFrom(x => x.flow_instance_id))
                .ForMember(des => des.Sequence, opt => opt.MapFrom(x => x.sequence))
                .ForMember(des => des.NodeId, opt => opt.MapFrom(x => x.node_id))
                .ReverseMap();

            CreateMap<JJFlowTemplateRemoteInterface, JJFlowTemplateRemoteInterfaceDropDownListResponse>().ReverseMap();
        }
    }

    public class JjFlowTrackVMResolver : IValueResolver<Node, JjFlowTrackVM, int>
    {
        public int Resolve(Node source, JjFlowTrackVM destination, int destMember, ResolutionContext context)
        {
            var tryParse = int.TryParse(source.to_node, out var nodeId);
            if (tryParse)
            {
                return nodeId;
            }

            return 0;
        }
    }
}